package com.qf.kdxf.controller;

import cn.hutool.json.JSONUtil;
import com.qf.kdxf.service.SpeechIdentifyService;
import com.qf.kdxf.util.common.UploadFile;
import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/speechidentify")
public class SpeechIdentifyController {

    @Autowired
    SpeechIdentifyService identifyService;

    /**
     * 01-语音听写(对应科大讯飞-语音识别-录音文件转写)
     * @param audioFile
     * @return
     */
    @RequestMapping("/identify")
    @ResponseBody
    public String SpeechTran(@RequestParam("audioFile") MultipartFile audioFile) {
        String text = null;
        try {
            String path = "D:\\codeStudy\\科飞接口\\kdxf(4)\\kdxf\\src\\main\\resources\\static\\TestAudio";
            String filePath = UploadFile.upload(audioFile,path);
            System.out.println(filePath);
            //调用API
            String result = identifyService.upload(filePath);
            //字符串进行转义。
            String jsonStr = StringEscapeUtils.unescapeJavaScript(result);
            //使用“JSONUtil.getByPath()”从“result” JSON 字符串中提取“orderId”
            String orderId = String.valueOf(JSONUtil.getByPath(JSONUtil.parse(jsonStr), "content.orderId"));
            //检索语音到文本翻译的最终文本结果
            text = identifyService.getResult(orderId);
        } catch (Exception e) {
            System.out.println("异常");
        }
        return text;
    }


}
